<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script>
			window.onload = function(){
				/* 遍历加监听
				var btns = document.getElementsByTagName("button")
				for (var i = 0, length = btns.length; i < length; i++) {
					var btn = btns[i]
					btn.index = i
					btn.onclick = function(){
						alert("这是第 " + (btn.index + 1) + " 个按钮")
					}
				}
				*/
				
				
				// 利用闭包
				var btns = document.getElementsByTagName("button")
				for (var i = 0, length = btns.length; i < length; i++) {
					;(function(i){
						var btn = btns[i]
						btn.onclick = function(){
							alert("这是第 " + (i + 1) + " 个按钮")
						}
					})(i)
				}
			}
		</script>
	</head>
	<body>
		<button>测试1</button>
		<button>测试2</button>
		<button>测试3</button>
	</body>
</html>
